<?php
defined( 'JOSCOM_VERSION' ) or die( 'Direct Access to this location is not allowed.' );
  if (!($acl->acl_check( 'administration', 'edit', 'users', $my->usertype, 'components', 'all' )
  | $acl->acl_check( 'administration', 'edit', 'users', $my->usertype, 'components', 'com_banners' ))) {
    //j15nc
  }
  if (defined( '_OSCOMMERCE_LOADED' )) {
    if (!DB_SHARED) {
      tep_db_connect() or die('Unable to connect to database server!');
    }
    global $category_links, $languages_id, $cPath, $cPath_array, $current_category_id;
    global $request_type, $lng, $cart, $currencies, $language, $navigation, $breadcrumb;
  } else {
    require(DIR_WS_COMPONENT . 'includes/application_top.php');
  }
  
  require(DIR_WS_COMPONENT .'includes/filenames.php');
  require(DIR_WS_COMPONENT .'includes/functions/verify_language.php');
  

$languages_folder_array = array();

$extension_array = array(	'image' => array("gif", "jpg", "png", "bmp", "tif","jpeg"),
							'php' => array("php"));
/*
 * Folder array for the pull down menu
 */
$folder_array = array(0=>array('id' => '', 'text' => ''),
					1=>array('id' => 'admin', 'text' => 'admin'),
					2=>array('id' => 'catalog', 'text' => 'catalog'));
				//	3=>array('id' => 'xoops', 'text' => 'xoops'));
			
$google_language_pair_array = array(0=> array("id" => "", "text" => "Please Choose"),									
									1=> array("id" => "ar|en", "text" => "Arabic to English"),
									2=> array("id" => "zh|en" ,"text" => "Chinese to English"),
									3=> array("id" => "zh-CN|zh-TW", "text" => "Chinese (Simplified to Traditional)"),
									4=> array("id" => "zh-TW|zh-CN", "text" => "Chinese (Traditional to Simplified)"),
									5=> array("id" => "nl|en" ,"text" => "Dutch to English"),
									6=> array("id" => "en|ar" ,"text" => "English to Arabic"),
									7=> array("id" => "en|zh-CN", "text" => "English to Chinese (Simplified)"),
									8=> array("id" => "en|zh-TW", "text" => "English to Chinese (Traditional)"),
									9=> array("id" => "en|nl", "text" => "English to Dutch"),
									10=> array("id" => "en|fr", "text" => "English to French"),
									11=> array("id" => "en|de", "text" => "English to German"),
									12=> array("id" => "en|el", "text" => "English to Greek"),
									13=> array("id" => "en|it", "text" => "English to Italian"),
									13=> array("id" => "en|ja", "text" => "English to Japanese"),
									15=> array("id" => "en|ko", "text" => "English to Korean"),
									16=> array("id" => "en|pt", "text" => "English to Portuguese"),
									17=> array("id" => "en|ru", "text" => "English to Russian"),
									18=> array("id" => "en|es", "text" => "English to Spanish"),
									19=> array("id" => "fr|en" ,"text" => "French to English"),
									20=> array("id" => "fr|de", "text" => "French to German"),
									21=> array("id" => "de|en" ,"text" => "German to English"),
									22=> array("id" => "de|fr", "text" => "German to French"),
									23=> array("id" => "el|en" ,"text" => "Greek to English"),
									24=> array("id" => "it|en", "text" => "Italian to English"),
									25=> array("id" => "ja|en", "text" => "Japanese to English"),
									26=> array("id" => "ko|en", "text" => "Korean to English"),
									27=> array("id" => "pt|en", "text" => "Portuguese to English"),
									28=> array("id" => "ru|en", "text" => "Russian to English"),
									29=> array("id" => "es|en", "text" => "Spanish to English")); 

				  


  
if($_GET['filename']){
	$filename = $_GET['filename'];
	$file_extension = findexts($_GET['filename']);
} else {
	$filename = '';
}


if($_GET['directory'] == 'admin'){
		$directory = DIR_WS_LANGUAGES;
		$directory_desc = $_GET['directory'];
		$image_directory = DIR_FS_LANGUAGES;		
} elseif ($_GET['directory'] == 'catalog'){
		//$directory = DIR_FS_CATALOG_LANGUAGES;
		$directory = DIR_FS_HTTP_ROOT . 'components/com_ecjc_lang/templates/joomlastyle/buttons/';
		$directory_desc = $_GET['directory'];
		$image_directory = DIR_WS_CATALOG . 'components/com_ecjc_lang/templates/joomlastyle/buttons/';
	//	$image_directory = "../".DIR_WS_LANGUAGES;
		//require(WS_TEMPLATES . E_TEMPLATE_FOLDER . '/'.$osMod.'.html');
} elseif ($_GET['directory'] == 'xoops'){
		$directory = DIR_FS_XOOPS_LANGUAGES;
		$directory_desc = $_GET['directory'];
		$image_directory = "../../../".DIR_WS_LANGUAGES;
} else {
		$directory = DIR_FS_XOOPS_LANGUAGES;
		$directory_desc = 'xoops';
		$image_directory = "../../../".DIR_WS_LANGUAGES;
}

							
if($_GET['action'] == 'backup'){
	$backup_directory = trim($directory,"/");
	$backup_directory .= "_backup/";
	full_copy($directory,$backup_directory);
}
					
			
global $database;
$database->setQuery("Select directory from #__osc_languages");
$result = $database->loadObjectList();
$numrows = count($result);
 

$languages_array = array();



foreach($result as $resultset){
array_push($languages_array, $resultset->directory);
}

$files = filelist($filename,$directory,1,0,'all',1,$extension_array['image']); 

$image_text_filename = 'buttons_image_text.php';
$fields = filelist($image_text_filename,$directory,1,0,'all',1,$extension_array['php']);


$files_language= array();

foreach ($files as $list) {
    //Builds the cross tab languages the files is available in
	if ($list['dir'] == 0){
    	
    	$files_language[$list['name']]['path'][$list['language']]= $list['path'];
    	$files_language[$list['name']]['path_ws'][$list['language']] = get_ws_from_fs_directory($list['path'],$directory);
    	$files_language[$list['name']][$list['language']] = 1;
    	$files_language[$list['name']]['base_url'] = str_replace($list['language'],"XXXXXX",$list['path']);
    }
	//Builds the Languages from the folder names	
	if (($list['level'] == 1) && ($list['dir']==1)) {
		$listname = $list['name'];
		array_push($languages_folder_array,$listname);
	}
} 

foreach ($fields as $list) {
    //Builds the languages the files is available in
	if ($list['dir'] == 0){
    
    	$fields_language['path'][$list['language']]= $list['path'];
    	$fields_language[$list['language']] = count($list['fields']);   	
    	if (count($list['fields'])>0){
    	    while(list($fieldname,$fieldtext) = each($list['fields'])){
    			$fields_language['fields'][$fieldname][$list['language']] = $fieldtext;
    		}    	
    	}

    }
} 


if(($_GET['action'] == 'copy') && ($filename)  && ($_GET['language'])){
	$language_copy_to = $_GET['language'];


	if(!file_exists(DIR_FS_CATALOG_LANGUAGES.$language_copy_to."/".$filename)){
		while(list($language_key,$val) = each($files_language[$filename])) {
			if($val == 1){
				copy(DIR_FS_CATALOG_LANGUAGES.$language_key."/".$filename,DIR_FS_CATALOG_LANGUAGES.$language_copy_to."/".$filename);
				$files = array();
				$files = filelist($filename,$directory,1,0,'all',1,'php'); 
				break;
			}
		}
		$files_language = array();
		foreach ($files as $list) {
		    //Builds the languages the files is available in
			if ($list['dir'] == 0){
	
		    	$files_language[$list['name']][$list['language']] = 1;
		    	$files_language[$list['name']]['path'] = $list['path'];
		    }
		}
		$fields = array();
		$fields = fieldlist($files);
	}
}

  
?>

<link rel="stylesheet" type="text/css" href="<?php echo DIR_WS_COMPONENT ?>includes/stylesheet.css">
<script language="javascript"><!--
function popupImageWindow(url) {
  window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=600,height=300,screenX=150,screenY=150,top=150,left=150')
}

function toggle(id) {
	var e = document.getElementById(id);
	if(e.style.display == 'none')
	e.style.display = 'block';
	else
	e.style.display = 'none';
}

//-->
</script>
<script type="text/javascript">
var http = false;

if(navigator.appName == "Microsoft Internet Explorer") {
	http = new ActiveXObject("Microsoft.XMLHTTP");
} else {
	http = new XMLHttpRequest();
}

function updateText(field,file,language,directory) {
                         
	document.getElementById(field+'_'+language+'_save').style.display = "none";
	document.getElementById(field+'_'+language+'_anim').style.display = "inline";
	var text_value = document.getElementById(field+'_'+language+'_text').value;
	
	http.abort();
	var url ='ajax_rewrite_fields.php?field='+field+'&file='+file+'&language='+lang+'&text='+encodeURI(text_value)+'&directory='+directory;
	http.open("GET", url, true);

	http.onreadystatechange=function() {
		if(http.readyState == 4) {
			if(http.responseText){
				document.getElementById(field+'_'+language+'_anim').style.display = "none";
				document.getElementById(field+'_'+language+'_save').style.display ="inline";
				if(http.responseText.indexOf('error')==-1){
						document.getElementById(field+'_'+language+'_top').innerHTML =http.responseText;
						document.getElementById(field+'_'+language).style.display = "none";
				}else{
						alert(http.responseText);
				}
			}
		}
	}
	http.send(null);
}

function getTranslation(field,language,language_pair) {
    //alert(field+'_select');                
	document.getElementById(field+'_save').style.display = "none";
	document.getElementById(field+'_anim').style.display = "inline";
	var text_value = encodeURI(document.getElementById(field+'_text').value);
	var language_pair = document.getElementById(field+'_select').value;          	
	http.abort();
	var url ='ajax_google_translate.php?text='+text_value+'&language_pair='+language_pair;
	http.open("GET", url, true);

	http.onreadystatechange=function() {
		if(http.readyState == 4) {
			if(http.responseText){
				document.getElementById(field+'_anim').style.display = "none";
				document.getElementById(field+'_save').style.display ="inline";
				if(http.responseText.indexOf('error')==-1){
					document.getElementById(field+'_text').value = http.responseText;
				}else{
					alert(http.responseText);
				}
			}
		}
	}
	http.send(null);
}

function generateButton(field,file,language,directory) {
    //alert(directory+file); 
	document.getElementById(field+'_'+language+'_save').style.display = "none";
	document.getElementById(field+'_'+language+'_anim').style.display = "inline";
	var text_value = document.getElementById(field+'_'+language+'_text').value;
	//http.abort();
	var url ='button.php?language='+language+'&text='+encodeURI(text_value)+'&save='+directory+file;
	//window.open(url);
	http.open("GET", url, true);

	http.onreadystatechange=function() {
		if(http.readyState == 4) {
			if(http.responseText){
				if(http.responseText=="error1"){
					alert('Error: Please check write permissions for your language image directories');
				} else {
					document.getElementById(field+'_'+language+'_anim').style.display = "none";
				document.getElementById(field+'_'+language+'_save').style.display ="inline";
				document.getElementById(field+'_'+language+'_imagediv').src='button.php?language='+language+'&text='+encodeURI(text_value);					
				}
			}
		}
	}
	http.send(null);
}
function getCopyIcon(fromIcon,toIcon, field, language, fromLang) {  
    document.getElementById(field+'_'+language+'_save').style.display = "none";
	document.getElementById(field+'_'+language+'_anim').style.display = "inline";
	var ranNum= Math.floor(Math.random()*5); 
	var url ='copy_icon.php?from='+fromIcon+'&to='+toIcon;
	//alert(fromLang);
	var new_path = document.getElementById(field+'_'+fromLang+'_imagediv').src
	//alert(new_path);
	//window.open(url);
	http.open("GET", url, true);

	http.onreadystatechange=function() {
		if(http.readyState == 4) {
			if(http.responseText){
				if(http.responseText=="error1"){
					alert('Error: Please check write permissions for your language image directories');
				} else if(http.responseText=="error3"){
					alert('Error: Image type suported are gif, jpg and png');
				}else if(http.responseText=="error2"){
					alert('Error: Language folders were not found');
				} else {
					document.getElementById(field+'_'+language+'_anim').style.display = "none";
					document.getElementById(field+'_'+language+'_save').style.display ="inline";
					document.getElementById(field+'_'+language+'_imagediv').src='';
					document.getElementById(field+'_'+language+'_imagediv').src=new_path;				
				}
			}
		}
	}
	http.send(null);
}
</script>



<script language="javascript"><!--
function popupWindow(url) {
  window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=600,height=300,screenX=150,screenY=150,top=150,left=150')
}
//--></script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
<div id="spiffycalendar" class="text"></div>
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
  <tr>
    <td width="<?php echo BOX_WIDTH; ?>" valign="top"><table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="1" cellpadding="1" class="columnLeft">
<!-- left_navigation //-->
<?php require(DIR_WS_INCLUDES . 'column_left.php'); ?>
<!-- left_navigation_eof //-->
    </table></td>
<!-- body_text //-->
    <td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
      <tr>
        <td><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
            <td><?php echo tep_draw_separator('pixel_trans.gif', HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT); ?></td>
            <td class="pageHeading" align="right"><?php echo TEXT_LANGUAGE_FOLDER_TO_CRAWL." ".tep_draw_form('languagefolder', tep_href_link(FILENAME_VERIFY_LANGUAGE_IMAGES, ''), 'get') . tep_draw_pull_down_menu('directory', $folder_array, '', 'onChange="this.form.submit();"') . tep_hide_session_id() . '</form>'; ?>
            </td>
          </tr>
          <tr>
          	<td colspan=2>
          		<b><?php echo TEXT_DIRECTORY; ?></b><?php echo "<b>".$directory_desc."</b> <span class='dataTableContent'>(".$directory.")</span>"; ?>
          	</td>
          	<td align="right">
          		&nbsp;&nbsp;&nbsp;<a href="<?php echo tep_href_link(FILENAME_VERIFY_LANGUAGE_IMAGES,"directory=".$directory_desc."&action=backup"); ?>"><?php echo TEXT_BACKUP_LANGUAGE_FILES; ?></a>
          	</td>
          </tr>
          <tr>
            <td colspan=2>
            &nbsp;&nbsp;&nbsp;<a href="<?php echo tep_href_link(FILENAME_VERIFY_LANGUAGE,"directory=".$directory_desc); ?>"><?php echo TEXT_LANGUAGE_FILES; ?></a>
            </td>
            <td class="pageHeading" align="right"><?php echo tep_draw_form('adminlanguage', FILENAME_DEFAULT, '', 'get') . tep_draw_pull_down_menu('language', $languages_array, $languages_selected, 'onChange="this.form.submit();"') . tep_hide_session_id() . '</form>'; ?></td>
          </tr>
        </table></td>
      </tr>
      <tr>
      <td>
<?php

?>

		<table width="100%" border=1>
		<th class="adminlist"></th>
		<?php 

			// Step 1.
			foreach($languages_folder_array as $lng) {
				
				echo "<th class='adminlist'>".$lng."</th>";
			}
			// Step 2.
			while (list($key, $files_lng) = each($files_language)){
				$new_key = str_replace(".", "___", $key);
				$base_url = $files_lng['base_url'];
				$filename = $key;
			
?>				
				<tr class='dataTableContent' >
					<td><b><?php echo $key; ?></b>
					</td>
<?php 			
				//print_r($languages_folder_array);echo "<br><br>";
				foreach($languages_folder_array as $lng){
					$id_base = $new_key.'_'.$lng;
					$base_url_lng = str_replace("XXXXXX",$lng,$base_url);

					$language_list = array();
					$index=0;
					 foreach($languages_folder_array as $language_item){
					 	if($language_item){
						 	$language_list[$index]['id']=urlencode(str_replace("XXXXXX",$language_item,$base_url).$key);
						 	$language_list[$index]['text']=$language_item;
					 		$index++;
					 	}
					 }	
					// echo"print out the languages $lng "; print_r($files_lng['path']);
?>						
					<td>
						<div id=<?php echo $id_base.'_image'; ?>>
							<img id=<?php echo $id_base.'_imagediv'; ?> src="<?php echo $files_lng['path_ws'][$lng].$key; ?>" alt="<?php echo $key;?>">
						</div>
						<br>
						<div id=<?php echo $id_base."_top>".($fields_language['fields'][$new_key][$lng]);?>
						</div>
						
						<br>
						
						<div style='float:right;'>
							<smalltext>
								<a href="#<?php echo $id_base;?>_image" onclick=javascript:toggle('<?php echo $id_base;?>')>
								<?php
									if ($files_lng[$lng]==1){
										// button exists
										echo tep_image(DIR_WS_IMAGES . 'icon_edit.gif', 'Modify', 16, 16);
									}else{
										//button does not exist
										echo tep_image(DIR_WS_IMAGES . 'icon_nonexist.gif', 'File Does Not Exist', 16, 16);
									} 
								?>
								</a>	
							</smalltext>
						</div>
						
						<div style='display:none;' id=<?php echo $id_base;?>>
							<a href="#<?php echo $id_base."_image"; ?>" onclick=javascript:generateButton('<?php echo $new_key."','".$key."','".$lng."','".urlencode($base_url_lng); ?>')>
							Generate Button
							</a> <br> 
							Copy from <?php echo tep_draw_pull_down_menu('language_copy', $language_list,'',"id=".$id_base."_copyfrom"); ?>	
							<a href="#<?php echo $id_base."_image";?>" onclick="javascript:getCopyIcon(document.getElementById('<?php echo$id_base;?>_copyfrom').value,'<?php echo urlencode($base_url_lng.$key)."','". $new_key."','".$lng ?>',document.getElementById('<?php echo$id_base;?>_copyfrom').options[document.getElementById('<?php echo$id_base;?>_copyfrom').selectedIndex].text);">
								<?php echo tep_image(DIR_WS_IMAGES . 'icon_upload.gif', 'Copy Over', 10, 10, 'id='.$id_base.'_copy'); ?>
							</a>
							<br>
							<textarea style='width:200px;' COLS='20' ROWS='3' id=<?php echo $id_base."_text"; ?>><?php echo htmlspecialchars($fields_language['fields'][$new_key][$lng]); ?></textarea>
							<a href="#<?php echo $id_base."_image"; ?>" onclick=javascript:updateText('<?php echo $new_key."','".$image_text_filename."','".$lng."','".urlencode($directory.$lng."/"); ?>')>
								<?php echo tep_image(DIR_WS_IMAGES . 'save_icon.png', 'Save', 16, 16, 'id='.$id_base.'_save'); ?>
							</a>
							<?php echo tep_image(DIR_WS_IMAGES . 'status_anim.gif', 'Processing', 16, 16, 'id='.$id_base.'_anim style="display:none;"'); ?>
							<br>
				    			Google Translate
				    		<br>
				    		<?php echo tep_draw_pull_down_menu('langauge_pair', $google_language_pair_array,'',"id=".$id_base."_select"); ?>			
							<a href="#<?php echo $id_base."_top";?>" onclick="javascript:getTranslation('<?php echo $id_base."','".$lng; ?>');">
								<?php echo tep_image(DIR_WS_IMAGES . 'icon_upload.gif', 'Translate', 20, 20, 'id='.$id_base.'_translate'); ?>
							</a>
			   			</div>
			   		</td>
<?php 
					
				}
				echo "</tr>";
			}
			// Step 3.

?>
		</table>
      </td>
      </tr>
    </table></td>
<!-- body_text_eof //-->
  </tr>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
<br>
<?php
  require(DIR_WS_INCLUDES . 'application_bottom.php');

  // -- webMS Modification --
  // reset the Mambo Database connection (stomped by osCommerce Connection)
  if (!DB_SHARED) {
    global $database, $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix;
    $database = new database( $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix );
  }
?>